<template>
  <!-- 接班确认 -->
  <view>
    <view class="form-container bor-top">
      <uni-forms ref="form" :modelValue="formData" :rules="rules" labelPosition="top">
        <!-- <view class="hidden-title">交接班信息</view> -->
        <cu-cell-group>
          <uni-forms-item name="takeoverSafetyAndEnvironmentUserId">
            <CuUserSelector
              label="安全主办"
              placeholder="选择人员"
              v-model="formData.takeoverSafetyAndEnvironmentUserId"
              required
            />
          </uni-forms-item>
          <uni-forms-item name="takeoverSyntheticalUserId">
            <CuUserSelector
              label="综合主办"
              placeholder="选择人员"
              v-model="formData.takeoverSyntheticalUserId"
              required
            />
          </uni-forms-item>
          <uni-forms-item name="takeoverBuildUserId">
            <CuUserSelector label="生产主办" placeholder="选择人员" v-model="formData.takeoverBuildUserId" required />
          </uni-forms-item>
          <uni-forms-item name="takeoverEquipmentUserId">
            <CuUserSelector
              label="设备主办"
              placeholder="选择人员"
              v-model="formData.takeoverEquipmentUserId"
              required
            />
          </uni-forms-item>
          <uni-forms-item name="takeoverOtherUser">
            <CuUserSelector
              label="其他参与人员"
              placeholder="选择人员"
              v-model="formData.takeoverOtherUser"
              required
              multiple
            />
          </uni-forms-item>
        </cu-cell-group>
        <view class="submit" @click="handleSubmit">完成</view>
      </uni-forms>
    </view>
  </view>
</template>

<script>
import CuCellGroup from '@/components/cu-cell/cu-cell-group.vue'
import CuCell from '@/components/cu-cell/cu-cell.vue'
import { takeoverMonitorConfirm } from '@/api/duty-basic/duty-shifting.js'
import CuUserSelector from '@/components/CuUserSelector/index.vue'

export default {
  name: 'StepShiftsMonitorConfirm',
  data() {
    return {
      formData: {
        changeShiftInfoId: '',
        processTaskId: '',
        takeoverBuildUserId: undefined, // 交班生产主办
        takeoverEquipmentUserId: undefined, // 交班设备主办
        takeoverOtherUser: [], // 交班其他人员array
        takeoverOtherUserId: [], // 交班其他人员array
        takeoverSafetyAndEnvironmentUserId: undefined, // 交班安全主办
        takeoverSyntheticalUserId: undefined // 交班综合主办
      },
      rules: {
        takeoverSyntheticalUserId: {
          rules: [
            {
              required: true,
              errorMessage: '综合主办不能为空'
            }
          ]
        },
        takeoverSafetyAndEnvironmentUserId: {
          rules: [
            {
              required: true,
              errorMessage: '安全主办不能为空'
            }
          ]
        },
        takeoverOtherUser: {
          rules: [
            {
              required: true,
              errorMessage: '其他人员不能为空'
            }
          ]
        },
        takeoverBuildUserId: {
          rules: [
            {
              required: true,
              errorMessage: '生产主办不能为空'
            }
          ]
        },
        takeoverEquipmentUserId: {
          rules: [
            {
              required: true,
              errorMessage: '设备主办不能为空'
            }
          ]
        }
      }
    }
  },
  methods: {
    async handleSubmit() {
      let curPage = getCurrentPages()
      let query = curPage[curPage.length - 1].options
      await this.$refs.form.validate()
      this.formData.takeoverOtherUser.map((item) => {
        this.formData.takeoverOtherUserId.push(item.userId)
      })
      this.formData.takeoverOtherUserId = this.formData.takeoverOtherUserId.join(',')
      this.formData.changeShiftInfoId = query.id
      this.formData.processTaskId = query.taskId
      await takeoverMonitorConfirm({
        ...this.formData
      })
      uni.showToast({
        title: '提交成功！',
        icon: 'success'
      })

      setTimeout(() => {
        uni.navigateBack()
      }, 1000)
    }
  },
  onReady() {
    // 需要在onReady中设置规则
    this.$refs.form.setRules(this.rules)
  },
  components: {
    CuCellGroup,
    CuCell,
    CuUserSelector
  }
}
</script>

<style lang="scss" scoped>
.submit {
  width: 600rpx;
  height: 88rpx;
  background: #454a5e;
  border-radius: 50rpx 50rpx 50rpx 50rpx;
  opacity: 1;
  text-align: center;
  line-height: 88rpx;
  font-size: 36rpx;
  font-weight: 400;
  color: #ffffff;
  margin: 0 auto;
  margin-top: 70rpx;
  margin-bottom: 70rpx;
}

.text-area {
  height: 300rpx;
}
</style>
